﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcHeatloss.Models;

namespace MvcHeatloss.Controllers
{
    public class RoomController : Controller
    {
        public ProjectEntities db = new ProjectEntities();

        Room room = new Room();
        //
        // GET: /Room/
        [HttpGet]
        public ActionResult Index()
        {
            Room room = new Room();
            ViewBag.Measurements = new SelectList(Room.MeasurementsList);
            ViewBag.TemperatureDifference = new SelectList(Room.TemperatureDifferenceList);
            ViewBag.AirChanges = new SelectList(Room.AirChangesList);
            ViewBag.NoOfWalls = new SelectList(Room.NoOfWallsList);

            return View(room);
        }

        [HttpPost]
        public ActionResult Index(Room room, string Measurements, string AirChanges, string TemperatureDifference, string NoOfWalls)
        {
            int temp = Int32.Parse(TemperatureDifference);
            int walls = Int32.Parse(NoOfWalls);
            double air = Double.Parse(AirChanges);
            if(room.Measurement == "Feet")
            {
                room.Height = (decimal)CalcHeatoss.CovertToMeters((double)room.Height);
                room.Length = (decimal)CalcHeatoss.CovertToMeters((double)room.Length);
                room.Width = (decimal)CalcHeatoss.CovertToMeters((double)room.Width);
            }
            
            Room room1 = new Room(air, temp, (double)room.Height, (double)room.Length, (double)room.Width, walls);
            
            if (room.Advanced)
            {
                return RedirectToAction("Step1", room);
            }
            else
            {
                return RedirectToAction("Radiator", room1);
            }
        }

        public ActionResult Step1(Room room)
        {
            ViewBag.WallTypes1 = new SelectList(Room.WallTypeList);
            ViewBag.WallTypes2 = new SelectList(Room.WallTypeList);
            ViewBag.WallTypes3 = new SelectList(Room.WallTypeList);
            ViewBag.WallTypes4 = new SelectList(Room.WallTypeList);
            ViewBag.WallTypes5 = new SelectList(Room.WallTypeList);
            ViewBag.WallTypes6 = new SelectList(Room.WallTypeList);
            ViewBag.WallTypes7 = new SelectList(Room.WallTypeList);
            ViewBag.WallTypes8 = new SelectList(Room.WallTypeList);
            ViewBag.CeilingType = new SelectList(Room.CeilingTypeList);
            ViewBag.FloorType = new SelectList(Room.FloorTypeList);
            ViewBag.WindowType = new SelectList(Room.WindowTypeList);
            ViewBag.DoorType = new SelectList(Room.DoorTypeList);
            ViewBag.SkylightType = new SelectList(Room.SkylightTypeList);

            return View(room);
        }

        //[HttpPost]
        //public ActionResult Step1(Room room, string WallTypes1, string WallTypes2, string WallTypes3, string WallTypes4, 
        //    string WallTypes5, string WallTypes6, string WallTypes7, string WallTypes8, string CeilingType, string FloorType,
        //    string WindowType, string DoorType, string SkylightType)
        //{

        //    return RedirectToAction("Radiator", room1);
        //}

        // GET: /Room/Radiators/
        public ActionResult Radiator(Room room1)
        {
            ViewBag.RequiredBTU = room1.RequiredBTU;
            var radiators = db.Radiators.Where(b => b.BTU >= room1.RequiredBTU).ToList();

            return View(radiators);
        }


        //
        // GET: /Room/Details/5

        public ActionResult Details(int id = 0)
        {
            Room room = db.Rooms.Single(r => r.RoomID == id);
            if (room == null)
            {
                return HttpNotFound();
            }
            return View(room);
        }

        //
        // GET: /Room/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Room/Create

        [HttpPost]
        public ActionResult Create(Room room)
        {
            if (ModelState.IsValid)
            {
                db.Rooms.AddObject(room);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(room);
        }

        //
        // GET: /Room/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Room room = db.Rooms.Single(r => r.RoomID == id);
            if (room == null)
            {
                return HttpNotFound();
            }
            return View(room);
        }

        //
        // POST: /Room/Edit/5

        [HttpPost]
        public ActionResult Edit(Room room)
        {
            if (ModelState.IsValid)
            {
                db.Rooms.Attach(room);
                db.ObjectStateManager.ChangeObjectState(room, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(room);
        }

        //
        // GET: /Room/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Room room = db.Rooms.Single(r => r.RoomID == id);
            if (room == null)
            {
                return HttpNotFound();
            }
            return View(room);
        }

        //
        // POST: /Room/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Room room = db.Rooms.Single(r => r.RoomID == id);
            db.Rooms.DeleteObject(room);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}